package com.campol.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

import com.campol.extlogger.ExtLogger;
import com.campol.model.Reporter;
import com.campol.utils.CustomXmlParser;

public class ServiceManager {

	public static String performRequest(String url) {
		String html = null;
		if (url != null) {
			url = url.replace(",", ".");
		}
		try {
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.preformRequest()", "Request: "+url);
			HttpGet httpGet = new HttpGet(url);
			HttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				
				InputStream istr = response.getEntity().getContent();

				BufferedReader reader = new BufferedReader(new InputStreamReader(istr));

				// Copy response to string buffer.
				StringBuffer buffer = new StringBuffer();
				String strContentLine = null;
				while ((strContentLine = reader.readLine()) != null) {
					buffer.append(strContentLine);
				}
				reader.close();

				html = buffer.toString();
				
				ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.preformRequest()", "Successful response");
				return html;
			}
		} catch (Exception e) {
			Log.e("ServiceManager.preformRequest()", e.getMessage());
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.preformRequest()", "Exception. "+e.toString());
			return null;
		}
		return null;
	}

	public static boolean postRequestOfWarning(String url){
		String output = null;
		if (url != null) {
			url = url.replace(",", ".");
		}
		try {
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.postRequestOfWarning()", "Request: "+url);
			HttpPost httpPost = new HttpPost(url);
			HttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				
				InputStream istr = response.getEntity().getContent();

				BufferedReader reader = new BufferedReader(new InputStreamReader(istr));

				// Copy response to string buffer.
				StringBuffer buffer = new StringBuffer();
				String strContentLine = null;
				while ((strContentLine = reader.readLine()) != null) {
					buffer.append(strContentLine);
				}
				reader.close();

				output = buffer.toString();
				
				ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.postRequestOfWarning()", "Successful response");
				return CustomXmlParser.parseXmlForAddedPoint(output);
			}
		} catch (Exception e) {
			Log.e("ServiceManager.postRequestOfWarning()", e.getMessage());
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.postRequestOfWarning()", "Exception. "+e.toString());
			return false;
		}
		return true;
	}

	public static boolean requestForRegisterUser(String url) {
		try {
			String response = performRequest(url);
			return CustomXmlParser.parseXmlForRegistratedUser(response);
		} catch (Exception e) {
			e.printStackTrace();
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.requestForRegisterUser()", "Exception. "+e.toString());
			return false;
		}
	}
	
	public static boolean requestForTokenIsValid(String url) {
		try {
			String response = performRequest(url);
			return CustomXmlParser.parseXmlForTokenIsValid(response);
		} catch (Exception e) {
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.requestForTokenIsValid()", "Exception. "+e.toString());
			e.printStackTrace();
			return false;
		}
	}
	
	public static String requestForUserToken(String url) {
		try {
			String response = performRequest(url);
			return CustomXmlParser.parseXmlForToken(response);
		} catch (Exception e) {
			e.printStackTrace();
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.requestForUserToken()", "Exception. "+e.toString());
			return null;
		}
	}
	
	public static ArrayList<Reporter> requestForReporters(String url) {
		try {
			String response  = performRequest(url);
			return CustomXmlParser.parseXmlForTopReporters(response);
		} catch (Exception e) {
			e.printStackTrace();
			ExtLogger.getInstance().writeLog(new Date(), "ServiceManager.requestForReporters()", "Exception. "+e.toString());
			return null;
		}
	}
}
